<?php

require_once('../../libraryfiles/config.php');
$include = '<script type="text/javascript" src="' . W_ROOT . '/libraryfiles/js_global.php"></script>
<script type="text/javascript" src="' . W_ROOT . '/js/action.js"></script>
<script type="text/javascript" src="' . W_ROOT . '/js/common.js"></script>	
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
';
$view = isset($_GET['view']) ? $_GET['view'] : '';
switch ($view) {
    case 'watermark':
        if (isset($_GET['view']) && $_GET['view'] == 'watermark') {
            $destinationFolder = '../../product/images/';
            $font = '../../fonts/arial.ttf';
            $sql = 'SELECT * FROM ' . Tblrefcode::tblname . ' WHERE ' . Tblrefcode::name . ' = ' . '"watermark"';
            $query = mysql_query($sql);
            $row = mysql_fetch_object($query);
            $text = $row->{Tblrefcode::value};
            generateWatermarkText($destinationFolder, $destinationFolder, $text, $font);
            header('location:' . base_url_admin . '/product/index.php');
            exit();
        }
        break;
    case 'delete':
        if (isset($_GET['view']) && $_GET['view'] == 'delete') {
            $id = $_GET['id'];
            $image = '';
            $img = mysql_query("SELECT * FROM " . Tblproduct::tblproduct . " WHERE " . Tblproduct::id . "=" . $id);
            while ($arr = mysql_fetch_object($img)) {
                $image = $arr->id;
            }
            $sql = "DELETE FROM " . Tblproduct::tblproduct . " WHERE " . Tblproduct::id . "=" . $id;
            $sql_image = mysql_query("SELECT *FROM tblimage WHERE product_id='" . $image . "'");
            while ($imgLinks = mysql_fetch_object($sql_image)) {
                if (file_exists(S_S_IMAGES_DIR . $imgLinks->images)) {
                    unlink(S_S_IMAGES_DIR . $imgLinks->images);
                }
                if (file_exists(S_S_IMAGES_DIR . 'thumb/' . $imgLinks->images)) {
                    unlink(S_S_IMAGES_DIR . 'thumb/' . $imgLinks->images);
                }
            }

            mysql_query("DELETE FROM tblimage WHERE product_id='" . $image . "'");
            mysql_query($sql);
            header('Location:' . base_url_admin . '/product/index.php?msg=delete_success');
            exit();
        }
        break;

    case 'add':
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
        } else if (isset($_POST['id'])) {
            $id = $_POST['id'][0];
        }
        else
            $id = 0;

        $content = 'add.php';
        break;

    case 'edit':
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
        } else if (isset($_POST['id'])) {
            $id = $_POST['id'][0];
        }
        else
            $id = 0;

        $content = 'add.php';
        break;

    case 'addexist':
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
        } else if (isset($_POST['id'])) {
            $id = $_POST['id'][0];
        }
        else
            $id = 0;

        $content = 'formaddexist.php';
        break;

    case 'editexist':
        if (isset($_GET['id'])) {
            $id = (int) $_GET['id'];
        } else if (isset($_POST['id'])) {
            $id = $_POST['id'][0];
        }
        else
            $id = 0;

        $content = 'formeditexist.php';
        break;

    case 'publish':
        if (isset($_GET['id']) && (int) $_GET['id'] > 0) {
            $id = (int) $_GET['id'];
        } else {
            header('Location:' . base_url_admin . '/product/index.php');
            exit();
        }

        if (isset($_GET['pharm_id']) && (int) $_GET['pharm_id'] >= 0) {
            $phar_id = (int) $_GET['pharm_id'];
        }

        if (isset($_GET['publish']) && (int) $_GET['publish'] >= 0 && (int) $_GET['publish'] <= 1) {
            $publish = (int) $_GET['publish'];
        } else {
            header('Location:' . base_url_admin . '/product/index.php');
            exit();
        }

        //Statement to publish/unpublish User
        publishPro('tblproduct_in_pharmacy', $publish, $id, $phar_id);

        header("Location:" . base_url_admin . "/product/index.php?" . getAllGetParams(array('view', 'id', 'publish'))); //page=".$page."&");
        exit();
        break;

    default:
        $content = 'list.php';
        break;
}
//require_once TMP_ADMIN_PATH;
require_once TMP_ADMIN_PATH;
?>